<?php
/******************************************************************************
 Available Customization
 created by Sivakumaran
 File Description:
 		To Show all customizations for a report and options provided to add ,edit,delete customizations
	 	
/*****************************************************************************/

include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/global_locale.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/report_php.inc");
include(DOCINCLUDES."global_files/tables.inc");
include(DOCINCLUDES."global_files/global_session.inc");
include(DOCINCLUDES."global_files/form_php.inc");
include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_validations.inc");
$db=new cDatabase();
$page=new cPage();
$form= new cForm();
if($_GET)
	{
	$report_id=$_GET["report_id"];
	$action=$_GET["action"];
	}
if($_POST)
	$report_id=$_POST["report_id"];
if($action=="delete")
	{
	$_POST["delete"][$_GET["customization_id"]]=$_GET["customization_id"];
	$_POST["report_id"]=$report_id;
	}
// if($_SESSION["guser_report_array"][$report_id]!="rem")
// 	{
// 	$error_message="Sorry,You do not have enough permission to access this page. Please contact your admin.";
// 	header("Location:./welcome.php?error_message=$error_message");
// 	exit;
// 	}
if($_POST)
	{
	$posted_array=$_POST["delete"];
	$db->query="begin work;";
	$db->runQuery();
	$i=0;
	if($_POST["clone"])
		{
		$customization_name=$_POST["customization_name"];
		if(strtolower($customization_name)!='default')
		{
		if($customization_name!="")
		{
		foreach($_POST["clone"] as $key=>$value)
				$customization_id=$key;
		$db->query="select
				report_id,
				user_id,
				privilege,
				permission

				from
				$sys_report_customization_table

				where
				customization_id=$customization_id";
		$db->runQuery();
		$custom_details_array=$db->returnArrays();

		$db->query="select
				filter_id,
				filter_display_name,
				filter_order,
				default_value

				from
				$sys_filter_customization_table

				where customization_id=$customization_id";
		$db->runQuery();
		$filter_array=$db->returnArrays();

		$db->query="select
				column_id,
				display_name,
				style,
				display_summary,
				decimal_places,
				format_details,
				format_date,
				display_order,
				is_group

				from
				$report_customization_table

				where
				customization_id=$customization_id";
		$db->runQuery();
		$column_array=$db->returnArrays();
global $company_id;
		$db->query="select nextval('$company_id.sys_report_cus_id_seq') as customization_id";
		$db->runQuery();
		$db_customization_id_array=$db->returnArrays();
		$new_customization_id=$db_customization_id_array[0]["customization_id"];

		$field_name="";
		$field_value="";
		if($custom_details_array[0]["user_id"]!="")
			{
			$field_name.="user_id ,";
			$field_value.="'".$custom_details_array[0]["user_id"]."',";
			}
		if($custom_details_array[0]["privilege"]!="")
			{
			$field_name.="privilege ,";
			$field_value.=$custom_details_array[0]["privilege"].",";
			}
		if($custom_details_array[0]["permission"]!="")
			{
			$field_name.="permission ,";
			$field_value.="'".$custom_details_array[0]["permission"]."',";
			}
		if($field_name)
			{
			$field_name	=",".substr($field_name,0,-1);
			$field_value	=",".substr($field_value,0,-1);
			}
		$report_id=$custom_details_array[0]["report_id"];
		$db->query="insert into
				$sys_report_customization_table
				(customization_id,
				report_id,
				customization_name
				$field_name)

				values
				($new_customization_id,
				$report_id,
				'$customization_name'
				$field_value)";
		$db->runQuery();

		$c=count($filter_array);
		for($i=0;$i<$c;$i++)
			{

			$filter_id	=$filter_array[$i]["filter_id"];
			$filter_display_name=$filter_array[$i]["filter_display_name"];
			$filter_order	=$filter_array[$i]["filter_order"];
			if($filter_array[$i]["default_value"])
				{
				$default_value	=",'".$filter_array[$i]["default_value"]."'";
				$field_name	=",default_value";
				}
			else
				{
				$default_value="";
				$field_name="";
				}
			$db->query="insert into
					$sys_filter_customization_table
					(customization_id,
					filter_id,
					filter_display_name,
					filter_order
					$field_name)

					values
					($new_customization_id,
					$filter_id,
					'$filter_display_name',
					$filter_order
					$default_value)";
			$db->runQuery();
			}

		$c=count($column_array);
		$field_name="";
		$field_value="";
		for($i=0;$i<$c;$i++)
			{

			if($column_array[$i]["style"]!="")
			{
			$field_name.="style ,";
			$field_value.="'".$column_array[$i]["style"]."',";
			}
			if($column_array[$i]["display_summary"]!="")
			{
			$field_name.="display_summary ,";
			$field_value.="'".$column_array[$i]["display_summary"]."',";
			}
			if($column_array[$i]["decimal_places"]!="")
			{
			$field_name.="decimal_places ,";
			$field_value.=$column_array[$i]["decimal_places"].",";
			}
			if($column_array[$i]["format_details"]!="")
			{
			$field_name.="format_details ,";
			$field_value.="'".$column_array[$i]["format_details"]."',";
			}
			if($column_array[$i]["format_date"]!="")
			{
			$field_name.="format_date ,";
			$field_value.="'".$column_array[$i]["format_date"]."',";
			}
			if($column_array[$i]["is_group"]!="")
			{
			$field_name.="is_group ,";
			$field_value.="'".$column_array[$i]["is_group"]."',";
			}
			if($field_name)
			{
			$field_name	=",".substr($field_name,0,-1);
			$field_value	=",".substr($field_value,0,-1);
			}


			$db->query="insert into
					$report_customization_table
					(column_id,
					display_name,
					display_order,
					customization_id
					$field_name
					)

					values
					(".$column_array[$i][column_id].",
					'".$column_array[$i][display_name]."',
					$i+1,
					".$new_customization_id."
					$field_value
					)";
			$db->runQuery();
			$field_name="";
			$field_value="";
			}
		}
		$message="Report Format Created Successfully.";
		}
		else
			{
				echo '<script>';
				echo 'alert(\'Default Name not allowed\');';
				echo '</script>';
			}
			
	}
	if($_POST["delete"])
		{
		foreach($posted_array as $key=>$value)
			{
			$db->query="delete from $sys_filter_customization_table where customization_id=$key";
			$db->runQuery();
			$db->query="delete from $report_customization_table where customization_id=$key";
			$db->runQuery();
			$db->query="delete from $sys_report_customization_table where customization_id=$key";
			$db->runQuery();
			$i++;
			}
			$db->query="select * from $sys_report_customization_table where is_active='t' and report_id=$report_id";
			$db->runQuery();
			$active_array=$db->returnArrays();
			if(!$active_array)
				{
				$db->query="update
						$sys_report_customization_table

						set
						is_active='t'

						where
						customization_name ilike('Default') and report_id=$report_id";
				$db->runQuery();
				}
			$message="Successfully deleted the report format";
		}
	if($db->result)
		{
		$db->query="commit work;";
		$db->runQuery();
		echo '<script>';
		echo 'alert(\''.$message.'\');';
		echo '</script>'; 
		if($_POST["delete"])
			{
				
			echo '<script>';
			echo "window.location='report_page.php?report_id=$report_id';";
			echo '</script>'; 
			exit;
			}
		else
			{
			echo '<script>';
			echo 'opener.location.reload();';
			echo 'window.close();';
			echo '</script>'; 
			exit;
			}
		//$message="$i Customization Deleted";
		}
	}
	
	
	$db->query="
			SELECT
			src.customization_id,
			src.customization_name,
			mr.report_name

			FROM
			$sys_report_customization_table src JOIN  $master_report_table mr
			ON src.report_id=mr.report_id where mr.report_id=$report_id
			ORDER by src.customization_id ;";

	$db->runQuery();
	$customization_details_array=$db->returnArrays();
$page->doCentreHtml();
echo "<div align=\"left\" style=\"background-color:#CCCCCC;height:3%;\">";
echo "<h3>Manage Customizations</h3></div>";	


$c=count($customization_details_array);
echo "<form name =\"delete_form\" action =\"$PHP_SELF\" method=\"post\">";
echo $form->generateInputTag("hidden",array("name"=>"customization_name"));
for($i=0;$i<$c;$i++)
	{
	$customization_id=$customization_details_array[$i]["customization_id"];
	$customization_name=$customization_details_array[$i]["customization_name"];
	$customization_details[$i]["Customization Name"]=
		"<a href =./report_page.php?customization_id=$customization_id&report_id=$report_id title=\"Click me to view customized report\">".$customization_name."</a>";

	if($customization_name!="Default")
		{
		$query_string="filters_customization.php?customization_id=$customization_id&action=edit&report_id=$report_id";
		$customization_details[$i]["Edit"]=
		"<a href =$query_string>Edit</a>";
		$customization_details[$i]["Select"]=
		"<input type=\"checkbox\" name=\"delete[$customization_id]\">";
		}
	else
		{
		$customization_details[$i]["Edit"]="--";
		$customization_details[$i]["Select"]="--";
		}
	$customization_details[$i]["Clone"]=$form->generateInputTag("submit",array("name"=>"clone[$customization_id]","class"=>"save_as","title"=>"duplicate this customization","value"=>"","onclick"=>"if((document.delete_form.customization_name.value=prompt('Customization Name',''))==''){alert('Customization Name Should not be empty');}"));
	}

echo "<div align=\"right\" style=\"background-color:#CCCCCC;height:5%;\">";

echo "<a href=\"$PHP_SELF\" style=\"top:5px;\" title=\"delete all checked\" onClick=\"doSubmit();\">Delete Selected</a>";
echo " | ";

echo "<a href='./filters_customization.php?action=insert&report_id=$report_id' title=\"Click to create new customization\">Add New</a>";

echo "</div><br>";
if($message)
	echo $message."<br>";
doHorizontalTable($customization_details,"60%","","",true,true,"","table_border1");
echo "</form>";
?>
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

function doSubmit()
	{
	
	var confirm_status=confirm('C o n f i r m ?');
	if(confirm_status)
		document.delete_form.submit();
	}
</script>